package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_seal;

@Service
public class BankSealService extends BaseService {
	@Autowired
	private SealSetService bService;
	
	public List<String> selectSealbank(String bank_id){
		
		List<String> ids = new ArrayList<String>();
	
		List<Map<String, Object>> bankList = selectBankByUseId(bank_id);
		for(int i=0;i<bankList.size();i++){
			ids.add(bankList.get(i).get("_id")+"");
		}
		return ids;
	}
	
	
	public List<Map<String, Object>> selectBankByUseId(String bank_id){
		String sql = "select t._id from t_seal t "
				+ "where t.status=0 and t.bank_id=? and t.flag in ('启用','停用') and t.flag_stop in ('暂时停用') order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql,new String[]{bank_id});
		return list;
	}
	
	@Transactional
	public void checkSealBankCode(String bank_id) {
		List<String> ids =  selectSealbank(bank_id);
		for(String id:ids){
			t_seal seal = this.findObjById(id, t_seal.class);
			if("使用中".equals(seal.getFlag_use())){
				seal.setFlag_change("机构已改动，请处理印章");
			}
			if("已领取".equals(seal.getFlag_use())){
				seal.setFlag_change("机构已改动，请处理印章");
				seal.setFlag_use("待归还");
			}
			if("未领取".equals(seal.getFlag_use())){
				seal.setFlag_change("机构已改动，请处理印章");
				seal.setFlag("停用");
			}
			if("停用".equals(seal.getFlag())){
				seal.setFlag_change("机构已改动，请处理印章");
				if(!bService.checkIsTop(id)){
					seal.setFlag_stop("上缴停用");
				}
			}
			updateObj(seal);
		}
	}
}
